import { createRouter, createWebHashHistory } from 'vue-router'
import store from '../store/index.js'

const routes = [
  //登陆页面
  {
    path:'/Login',
    name:'Login',
    component: ()=>import('../views/pages/Login.vue')
  },    
  {
    path: '/',
    name: 'Layout',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../views/Layout/Layout.vue'),
    redirect: "/index",  //重定向（默认初始页面是角色列表页）
    // 嵌套路由  ||  子路由
    children:[ 
      // 首页
      {
        path:'/index',
        name:'index',
        component: () => import('../views/pages/index.vue')
      },
      {
        // 用户列表页
        path:'/roles',
        name:'roles',
        component: () => import('../views/pages/rolesList')
      },
      {
        // 权限管理
        path:'/jurisdiction',
        name:'jurisdiction',
        component: () => import('../views/pages/jurisdiction.vue')
      },
      {
        // 商品管理
        path:'/commodity',
        name:'commodity',
        component: () => import('../views/pages/commodity.vue')
      },
      {
        // 订单管理
        path:'/order',
        name:'order',
        component: () => import('../views/pages/order.vue')
      },
      {
        // 数据统计
        path:'/data_statistics',
        name:'data_statistics',
        component: () => import('../views/pages/data_statistics.vue')
      }
    ]
  }
]
//生成hash路由对象
const router = createRouter({
  history: createWebHashHistory(),
  routes
})
//路由守卫
router.beforeEach((to,from,next)=>{
  // to: 从哪个页面
  // from: 到哪个页面
  // next: 只有执行next()页面

  //判断用户是否登录
  if(!sessionStorage.getItem('orgCode')){
    // 未登录，跳转到login页面
    if( to.path === '/Login'){
      next()
      return
    }
    next('/Login')
  }else{
    // 已登录
    next()
  }
})
export default router
